package com.example.yyh.recorddemo.permission;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.os.Build;
//import android.support.annotation.NonNull;
//import android.support.v4.app.AppOpsManagerCompat;
//import android.support.v4.app.Fragment;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.core.app.AppOpsManagerCompat;
import androidx.fragment.app.Fragment;

import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;

import java.util.List;


/**
 * Created by yaoyihang on 2018/12/1
 */
public class PermissionManager {

    /**
     * 请求的权限已同意
     */
    public static final int ALLOWED = 0;

    /**
     * 权限处于默认状态，及使用到某功能时会对权限进行询问
     */
    public static final int DEFAULT = 1;

    /**
     * 请求的权限被拒绝
     */
    public static final int IGNORED = 2;

    /**
     *
     * @param activity activity实例
     * @param requestCode 请求码
     * @param permissions 权限
     */
    public static void requestPermission(@NonNull Activity activity, int requestCode, @NonNull String... permissions) {
        AndPermission.with(activity)
                .requestCode(requestCode)
                .permission(permissions)
                .send();
    }

    /**
     *
     * @param fragment fragment实例
     * @param requestCode 请求码
     * @param permissions 权限
     */
    public static void requestPermission(@NonNull Fragment fragment, int requestCode, @NonNull String... permissions) {
        AndPermission.with(fragment)
                .requestCode(requestCode)
                .permission(permissions)
                .send();
    }

    /**
     * 用户申请权限后的结果处理
     */
    public static void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                                  @NonNull int[] grantResults, PermissionListener permissionListener){
        AndPermission.onRequestPermissionsResult(requestCode, permissions, grantResults, permissionListener);
    }

    /**
     * 授权失败
     */
    public static void onFailed(@NonNull Fragment fragment, List<String> deniedPermissions, int requestCode) {
        if (AndPermission.hasAlwaysDeniedPermission(fragment, deniedPermissions)) {
            AndPermission.defaultSettingDialog(fragment, requestCode).show();
        } else {
            AndPermission.defaultSettingDialog(fragment, requestCode).show();
        }
    }

    /**
     * 授权失败
     */
    public static void onFailed(@NonNull Activity activity, List<String> deniedPermissions, int requestCode) {
        if (AndPermission.hasAlwaysDeniedPermission(activity, deniedPermissions)) {
            AndPermission.defaultSettingDialog(activity, requestCode).show();
        } else {
            AndPermission.defaultSettingDialog(activity, requestCode).show();
        }
    }

    /**
     *
     * @param context       {@link Context}
     * @param permissions   申请的权限
     * @return              权限授权状态
     */
    public static int permissionToOp(@NonNull Context context, @NonNull String... permissions) {
        for (String permission : permissions) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                String op = AppOpsManagerCompat.permissionToOp(permission);
                int result = AppOpsManagerCompat.noteProxyOp(context, op, context.getPackageName());
                if (result == AppOpsManagerCompat.MODE_IGNORED) {
                    return IGNORED;
                } else if (result == AppOpsManagerCompat.MODE_DEFAULT) {
                    return DEFAULT;
                }
            } else {
                if(TextUtils.equals(permission, Manifest.permission.CAMERA)) {
                    if(!isCameraCanUse()) {
                        return IGNORED;
                    }
                }
            }
        }
        return ALLOWED;
    }

    /**
     * 判断6.0一下手机当前相机权限是否可用
     *
     * @return  相机可用状态
     */
    public static boolean isCameraCanUse() {
        boolean canUse = true;
        Camera mCamera = null;
        try {
            mCamera = Camera.open();
            // 对象不为null
            Camera.Parameters mParameters = mCamera.getParameters();
            mCamera.setParameters(mParameters);
        } catch (Exception e) {
            canUse = false;
        }
        if (mCamera != null) {
            mCamera.release();
        }
        return canUse;
    }
}
